#include "BlockQueue.hpp"
#include <iostream>
#include <pthread.h>
#include <unistd.h>

int cnt = 0;
std::vector<int> view;

void* consumer(void* args)
{
    BlockQueue<int>* bq = static_cast<BlockQueue<int>*> (args);

    while(true)
    {
        sleep(2);
        cnt--;
        bq->Pop();
        view.pop_back();
        std::cout << "处理了一个任务" << std::endl;
        std::cout << "任务列表现如下:" << std::endl;
        for(auto& e:view)
        {
            printf("%d ", e);
        }
        std::cout << std::endl;
    }
    
}

void* productor(void* args)
{
    BlockQueue<int>* bq = static_cast<BlockQueue<int>*> (args);
    
    while(true)
    {
        cnt++;
        bq->Equeue(cnt);
        view.push_back(cnt);
        std::cout << "生产了一个任务" << std::endl;
        std::cout << "任务列表现如下:" << std::endl;
        for(auto& e:view)
        {
            printf("%d ", e);
        }
        std::cout << std::endl;
    }
}

int main()
{
    BlockQueue<int> *bq = new BlockQueue<int>();
    
    pthread_t c[3], p[2];

    pthread_create(c, nullptr, consumer, bq);
    pthread_create(c + 1, nullptr, consumer, bq);
    pthread_create(c + 2, nullptr, consumer, bq);
    pthread_create(p, nullptr, productor, bq);
    pthread_create(p + 1, nullptr, productor, bq);
    
    pthread_join(*c, nullptr);
    pthread_join(*(c + 1), nullptr);
    pthread_join(*(c + 2), nullptr);
    
    pthread_join(*p, nullptr);
    pthread_join(*(p + 1), nullptr);


    return 0;
}